package com.app.mapper;

import com.app.model.InviteRecordModel;
import com.app.model.UserModel;
import com.app.vo.RankingVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 邀请记录
 *
 * @author 16693103420
 * @since 2024-02-03
 */
@Mapper
public interface InviteRecordMapper extends BaseMapper<InviteRecordModel> {
    //根据时间范围进行查询邀请人数
    @Select("select count(*) from tb_invite_record where DATE_FORMAT(create_time,\"%Y-%m-%d %H:%i:%s\")BETWEEN #{beginTime}and #{endTime} and invite_id=#{userId}")
    Integer promotionTime(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("userId") Integer userId);

    //本月此用户第一条数据
    @Select("select * from tb_invite_record where DATE_FORMAT(create_time,\"%Y-%m-%d %H:%i:%s\")BETWEEN #{beginTime}and #{endTime} and invite_id=#{userId} ORDER BY create_time desc limit 1 ")
    InviteRecordModel firstMonthInviteRecord(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("userId") Integer userId);

    //所有记录中第一条数据
    @Select("select * from tb_invite_record where invite_id=#{userId} ORDER BY create_time desc limit 1 ")
    InviteRecordModel firstInviteRecord(@Param("userId") Integer userId);

    //根据邀请人ID查询被邀请人ID列表
    @Select("select quilt_invite_id from tb_invite_record where invite_id=#{inviteId}")
    List<Integer> quiltInviteIdList(@Param("inviteId") Integer inviteId);


    //根据邀请人ID查询被邀请人ID列表
    @Select(" select u.* \n" +
            " from tb_invite_record r\n" +
            " left join tb_user u on r.quilt_invite_id = u.id\n" +
            " where invite_id=#{inviteId}")
    IPage<UserModel> quiltInviteUserList(Page page, @Param("inviteId") Integer inviteId);


    @Select({
            "<script> ",
            "SELECT u.id, u.name, u.phone, u.voucher, u.integral, u.face_url, u.vip, u.status,r.status as inviteStatus,",
            "r.create_time, CONCAT(c.city, ' / ', a.area) as cityName ",
            "FROM tb_invite_record r ",
            "LEFT JOIN tb_user u ON r.quilt_invite_id = u.id ",
            "LEFT JOIN tbc_cities c ON c.city_id = u.city_id ",
            "LEFT JOIN tbc_areas a ON a.area_id = u.area_id ",
            "WHERE r.invite_id = #{userId} ",
            "<if test='startTime != null and endTime != null  and startTime!= \"\" and endTime!= \"\"'> ",
            "AND r.create_time BETWEEN #{startTime} AND #{endTime} ",
            "</if> ",
            "<if test='name != null and name != \"\"'> ",
            "AND u.name LIKE CONCAT('%', #{name}, '%') ",
            "</if>" +
            "ORDER BY r.create_time DESC ",
            "</script>"
    })
    IPage<UserModel> queryLoginUserInvitationRecord(Page<UserModel> page,
                                                            @Param("userId") Integer userId,
                                                            @Param("startTime") String startTime,
                                                            @Param("endTime") String endTime,
                                                            @Param("name") String name);


}
